package com.cskaoyan.market.controller;

import com.cskaoyan.market.db.domain.MarketCoupon;
import com.cskaoyan.market.db.domain.MarketCouponUser;

import com.cskaoyan.market.service.MarketAdminCouponService;
import com.cskaoyan.market.service.MarketCouponService;

import com.cskaoyan.market.service.impl.MarketCouponServiceImpl;
import com.cskaoyan.market.util.GetRequestBodyUtil;
import com.cskaoyan.market.util.HttpUtil;
import com.cskaoyan.market.util.JacksonUtil;
import com.cskaoyan.market.util.ResponseUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.github.pagehelper.PageHelper;

import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;

@WebServlet("/admin/coupon/*")
public class AdminCouponController extends HttpServlet {
    private MarketCouponService marketCouponService = new MarketCouponServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String op = getOpString(req);

        //查询优惠券接口，本身属于个人接口，但需要先实现才能检验其他coupon团队接口，在此先实现
        if ("list".equals(op)) {
            list(req, resp);
        }
//        查询用户前，读取优惠券接口
        if ("read".equals(op)) {
            read(req, resp);
        }
        //查询用户接口
        if ("listuser".equals(op)) {
            listUser(req, resp);
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String opString = getOpString(req);
        //编辑优惠券接口
        if ("update".equals(opString)) {
            update(req, resp);
        }
//        删除优惠券接口
        if ("delete".equals(opString)) {
            delete(req, resp);
        }

        if("create".equals(opString)){
            create(req,resp);
        }
    }

    private void create(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String requestBody = HttpUtil.getRequestBody(req);
        MarketCoupon marketCoupon = JacksonUtil.getObjectMapper().readValue(requestBody, MarketCoupon.class);
        marketCouponService.create(marketCoupon);
        resp.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(ResponseUtil.ok(marketCoupon)));
    }

    private void read(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
        MarketCoupon readCoupon = marketCouponService.readCoupon(id);
        String respBody = JacksonUtil.writeValueAsString(ResponseUtil.ok(readCoupon));
        resp.getWriter().println(respBody);
    }

    private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String page = req.getParameter("page");
        String limit = req.getParameter("limit");
        String name = req.getParameter("name");
        String type = req.getParameter("type");
        String status = req.getParameter("status");
        String sort = req.getParameter("sort");
        String order = req.getParameter("order");
        List<MarketCoupon> list=marketCouponService.list(page,limit,name,type,status,sort,order);
        System.out.println(list);
        resp.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(ResponseUtil.okList(list)));
    }


    private void listUser(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String page = req.getParameter("page");
        String limit = req.getParameter("limit");
        String couponId = req.getParameter("couponId");
        String userId = req.getParameter("userId");
        String status = req.getParameter("status");
        String sort = req.getParameter("sort");
        String order = req.getParameter("order");
        List<MarketCouponUser> couponUsers = marketCouponService.listUser(page, limit, couponId, userId, status, sort, order);
        Object o = ResponseUtil.okList(couponUsers);
        String string = JacksonUtil.writeValueAsString(o);
        resp.getWriter().println(string);
    }

    private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String requestBody = GetRequestBodyUtil.getRequestBody(req);
        MarketCoupon marketCoupon = JacksonUtil.getObjectMapper().readValue(requestBody, MarketCoupon.class);
        MarketCoupon updatedCoupon = marketCouponService.updateCoupon(marketCoupon);
        String respBody = JacksonUtil.writeValueAsString(ResponseUtil.ok(updatedCoupon));
        resp.getWriter().println(respBody);
    }

    private void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String requestBody = GetRequestBodyUtil.getRequestBody(req);
        MarketCoupon marketCoupon = JacksonUtil.getObjectMapper().readValue(requestBody, MarketCoupon.class);
        marketCouponService.deleteCoupon(marketCoupon);
        String respBody = JacksonUtil.writeValueAsString(ResponseUtil.ok());
        resp.getWriter().println(respBody);
    }

    //分发请求
    private String getOpString(HttpServletRequest req) {
        String requestURI = req.getRequestURI();
        return requestURI.replace(req.getContextPath() + "/admin/coupon/", "");
    }


}
